﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DECOR.BL.BC;
using System.Collections.Generic;
using DECOR.BL.BE;
using Newtonsoft.Json;

namespace DECOR.Touchapp
{
    public partial class crearPlanFacturacion : System.Web.UI.Page
    {
        string pNumPedido = "";
        string pPlanFacturacion = ""; //lista de plan de facturacion, desde sencha se envia como objeto
        string Json = "";
        string pRetval = "";
        List<PlanFactBE> lstPlanFact = new List<PlanFactBE>();
        decimal pTotal = 0m;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Request.HttpMethod.Equals("POST"))
            {
                
            }
            else
            {
                CredencialesController.cargarInfo(Request.Params); //(SIEMPRE)
                pNumPedido = Request.Params["pNumPedido"] != null ? Request.Params["pNumPedido"] : ""; //"0000003846"; // 
                pPlanFacturacion = Request.Params["pPlanFacturacion"] != null ? Request.Params["pPlanFacturacion"] : "";
                pTotal = Request.Params["pTotal"] != null ? Convert.ToDecimal(Request.Params["pTotal"]) : 0m;

                pRetval = "{\"success\": {0}, \"errors\":{\"reason\": \"{1}\"}, ";
                pRetval += "\"planFact\": {2}, \"CatFactBE\": {3} ";
                pRetval += "}";

                CrearPlanFacturacion();
                PlanFacturacion(pNumPedido);

                Response.ContentType = "application/json; charset=UTF-8";
                Response.Write(pRetval);
            }
        }

        private void CrearPlanFacturacion()
        {
            PedidoBC.Instance.setPlanFact(pNumPedido, leerListaPlanFact(pPlanFacturacion), true);
        }

        private void PlanFacturacion(string pNumPedido)
        {
            List<PlanFactBE> planFact = null;
            List<CatFactBE> CatFactBE;

            pNumPedido = pNumPedido.PadLeft(10, '0');
            CatFactBE = new List<CatFactBE>();
            planFact = PedidoBC.Instance.getPlanFact(pNumPedido, out CatFactBE);
            //adicionamos un item a la lista
            addItem(planFact);
            if (planFact != null)
            {
                Json = JsonConvert.SerializeObject(planFact);
                pRetval = pRetval.Replace("{0}", "true");
                pRetval = pRetval.Replace("{1}", "");
                pRetval = pRetval.Replace("{2}", Json);
                if (CatFactBE != null)
                {
                    Json = JsonConvert.SerializeObject(CatFactBE);
                    pRetval = pRetval.Replace("{3}", Json);
                }
                else
                {
                    pRetval = pRetval.Replace("{3}", "null");
                }
            }
            else
            {
                Json = JsonConvert.SerializeObject(planFact);
                pRetval = pRetval.Replace("{0}", "false");
                pRetval = pRetval.Replace("{1}", "No se encontró información");
                pRetval = pRetval.Replace("{2}", "null");
                pRetval = pRetval.Replace("{3}", "null");
            }
        }

        private void addItem(List<PlanFactBE> _planFact)
        {
            List<PlanFactBE> planFact = null;
            planFact = _planFact;
            decimal porcentaje = 0;
            bool add = true;
            foreach (PlanFactBE pf in planFact)
            {
                porcentaje += decimal.Round(pf.FPROZ / 100, 3);
                add = planFact[planFact.Count - 1].FKDAT.Year.Equals(1) ? false : true;
            }
            if (porcentaje < 1 && add)
            {
                if (planFact == null)
                {
                    planFact = new List<PlanFactBE>();
                }
                PlanFactBE planFactBE = new PlanFactBE();

                if (planFact.Count == 0)
                {
                    planFactBE.FPROZ = 0;
                    planFactBE.FAKWR = planFactBE.FPROZ * pTotal;
                }
                else
                {
                    planFactBE.FPROZ = (1 - porcentaje) * 100;
                    planFactBE.FAKWR = (1 - porcentaje) * pTotal;
                }
                planFactBE.TAXK1 = "I";

                if (planFact.Count > 0)
                    planFactBE.FPLTR = (int.Parse(planFact[planFact.Count - 1].FPLTR) + 1).ToString().PadLeft(6, '0');
                else
                    planFactBE.FPLTR = "000001";

                planFact.Add(planFactBE);
            }
        }

        private List<PlanFactBE> leerListaPlanFact(string _listaPlanFact)
        {
            List<PlanFactBE> lstP = new List<PlanFactBE>();
            PlanFactBE objP = new PlanFactBE();
            _listaPlanFact = _listaPlanFact.Replace("[", "").Replace("\"", "").Replace("{", "").Replace("]", "");

            string[] objStr = _listaPlanFact.Split('}');
            foreach (var itemStr in objStr)
            {
                itemStr.ToString().Replace("}", "");
                string[] obj = itemStr.Split(',');
                foreach (var item in obj)
                {
                    int pos = item.IndexOf(':');
                    string campo = item.Substring(0, pos);
                    string valor = "";
                    if (((item.Length - 1) - campo.Length) == 0)
                    {
                        valor = item.Substring(pos, (item.Length - 1) - campo.Length);
                    }
                    else
                    {
                        valor = item.Substring(pos + 1, (item.Length - 1) - campo.Length);
                    }
                    //leemos la cadena
                    if (campo == "FPLTR")
                    {
                        objP.FPLTR = valor.Trim();
                    }
                    else if (campo == "FKDAT")
                    {
                        objP.FKDAT = Convert.ToDateTime(valor.Trim().Substring(0, 10));
                    }
                    else if (campo == "id")
                    {
                        objP.AFDAT = valor.Trim() == "null" ? Convert.ToDateTime("01/01/1900") : Convert.ToDateTime(valor.Trim().Substring(0, 10));
                    }
                    else if (campo == "TETXT")
                    {
                        objP.TETXT = valor.Trim();
                    }
                    else if (campo == "FPROZ")
                    {
                        objP.FPROZ = Convert.ToDecimal(valor.Trim());
                    }
                    else if (campo == "FAKWR")
                    {
                        objP.FAKWR = Convert.ToDecimal(valor.Trim());
                    }
                    else if (campo == "WAERS")
                    {
                        objP.WAERS = valor.Trim();
                    }
                    else if (campo == "FAKSP")
                    {
                        objP.FAKSP = valor.Trim();
                    }
                    else if (campo == "FAREG")
                    {
                        objP.FAREG = valor.Trim();
                    }
                    else if (campo == "FKSAF")
                    {
                        objP.FKSAF = valor.Trim();
                    }
                    else if (campo == "ZTERM")
                    {
                        objP.ZTERM = valor.Trim();
                    }
                    else if (campo == "FPTTP")
                    {
                        objP.FPTTP = valor.Trim();
                    }
                    else if (campo == "FKARV")
                    {
                        objP.FKARV = valor.Trim();
                    }
                    else if (campo == "FPFIX")
                    {
                        objP.FPFIX = valor.Trim();
                    }
                    else if (campo == "KURFP")
                    {
                        objP.KURFP = Convert.ToDecimal(valor.Trim());
                    }
                    else if (campo == "PERIO")
                    {
                        objP.PERIO = valor.Trim();
                    }
                    else if (campo == "FPLAE")
                    {
                        objP.FPLAE = valor.Trim();
                    }
                    else if (campo == "MLSTN")
                    {
                        objP.MLSTN = valor.Trim();
                    }
                    else if (campo == "MLBEZ")
                    {
                        objP.MLBEZ = valor.Trim();
                    }
                    else if (campo == "KUNRG")
                    {
                        objP.KUNRG = valor.Trim();
                    }
                    else if (campo == "TAXK1")
                    {
                        objP.TAXK1 = valor.Trim();
                    }
                    else if (campo == "TAXK2")
                    {
                        objP.TAXK2 = valor.Trim();
                    }
                    else if (campo == "TAXK3")
                    {
                        objP.TAXK3 = valor.Trim();
                    }
                    else if (campo == "TAXK4")
                    {
                        objP.TAXK4 = valor.Trim();
                    }
                    else if (campo == "TAXK5")
                    {
                        objP.TAXK5 = valor.Trim();
                    }
                    else if (campo == "TAXK6")
                    {
                        objP.TAXK6 = valor.Trim();
                    }
                    else if (campo == "TAXK7")
                    {
                        objP.TAXK7 = valor.Trim();
                    }
                    else if (campo == "TAXK8")
                    {
                        objP.TAXK8 = valor.Trim();
                    }
                    else if (campo == "TAXK9")
                    {
                        objP.TAXK9 = valor.Trim();
                    }
                    else if (campo == "VALTG")
                    {
                        objP.VALTG = valor.Trim();
                    }
                    else if (campo == "VALDT")
                    {
                        objP.VALDT = valor.Trim() == "null" ? Convert.ToDateTime("01/01/1900") : Convert.ToDateTime(valor.Trim().Substring(0, 10));
                    }
                    else if (campo == "NFDAT")
                    {
                        objP.NFDAT = valor.Trim() == "null" ? Convert.ToDateTime("01/01/1900") : Convert.ToDateTime(valor.Trim().Substring(0, 10));
                    }
                    else if (campo == "TEMAN")
                    {
                        objP.TEMAN = valor.Trim();
                    }
                    else if (campo == "FAKCA")
                    {
                        objP.FAKCA = valor.Trim();
                    }
                    else if (campo == "NETWR")
                    {
                        objP.NETWR = Convert.ToDecimal(valor.Trim());
                    }
                    else if (campo == "NETPR")
                    {
                        objP.NETPR = Convert.ToDecimal(valor.Trim());
                    }
                    else if (campo == "WAVWR")
                    {
                        objP.WAVWR = Convert.ToDecimal(valor.Trim());
                    }
                    else if (campo == "KZWI1")
                    {
                        objP.KZWI1 = Convert.ToDecimal(valor.Trim());
                    }
                    else if (campo == "KZWI2")
                    {
                        objP.KZWI2 = Convert.ToDecimal(valor.Trim());
                    }
                    else if (campo == "KZWI3")
                    {
                        objP.KZWI3 = Convert.ToDecimal(valor.Trim());
                    }
                    else if (campo == "KZWI4")
                    {
                        objP.KZWI4 = Convert.ToDecimal(valor.Trim());
                    }
                    else if (campo == "KZWI5")
                    {
                        objP.KZWI5 = Convert.ToDecimal(valor.Trim());
                    }
                    else if (campo == "KZWI6")
                    {
                        objP.KZWI6 = Convert.ToDecimal(valor.Trim());
                    }
                    else if (campo == "CMPRE")
                    {
                        objP.CMPRE = Convert.ToDecimal(valor.Trim());
                    }
                    else if (campo == "SKFBP")
                    {
                        objP.SKFBP = Convert.ToDecimal(valor.Trim());
                    }
                    else if (campo == "BONBA")
                    {
                        objP.BONBA = Convert.ToDecimal(valor.Trim());
                    }
                    else if (campo == "PRSOK")
                    {
                        objP.PRSOK = valor.Trim();
                    }
                    else if (campo == "TYPZM")
                    {
                        objP.TYPZM = valor.Trim();
                    }
                    else if (campo == "CMPRE_FLT")
                    {
                        objP.CMPRE_FLT = Convert.ToDouble(valor.Trim());
                    }
                    else if (campo == "UELNR")
                    {
                        objP.UELNR = valor.Trim();
                    }
                    else if (campo == "UELTR")
                    {
                        objP.UELTR = valor.Trim();
                    }
                    else if (campo == "KURRF")
                    {
                        objP.KURRF = Convert.ToDecimal(valor.Trim());
                    }
                    else if (campo == "CCACT")
                    {
                        objP.CCACT = valor.Trim();
                    }
                    else if (campo == "KORTE")
                    {
                        objP.KORTE = valor.Trim();
                    }
                    else if (campo == "OFKDAT")
                    {
                        objP.OFKDAT = valor.Trim() == "null" ? Convert.ToDateTime("01/01/1900") : Convert.ToDateTime(valor.Trim().Substring(0, 10));
                    }
                    else if (campo == "FPLNR")
                    {
                        objP.FPLNR = valor.Trim();
                    }
                }
                lstP.Add(objP);
            }
            return lstP;
        }
    }
}
